import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
import {AuthJwtService} from '../../core/auth/auth-jwt.service';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
  loginForm: FormGroup;
  scopes = [
    {k: 'tlm', v: '塔里木'},
    {k: 'txn', v: '塔西南'},
    {k: 'ptr', v: '乙方单位'},
    {k: 'nj', v: '内建'}
  ];

  constructor(private fb: FormBuilder, private authJwtService: AuthJwtService) {
    this.loginForm = this.createLoginForm();
    this.loginForm.controls['username'].valueChanges.subscribe(value => console.log(value));
    this.loginForm.controls['password'].valueChanges.subscribe(value => console.log(value));
    this.loginForm.controls['scope'].valueChanges.subscribe(value => console.log(value));
    this.loginForm.controls['username'].setValue('jwj', {emitEvent: false});
    console.log(this.loginForm.getRawValue());
    console.log(this.loginForm.controls);
  }

  ngOnInit() {
  }

  private createLoginForm() {
    return new FormGroup({
      username: new FormControl('', Validators.required),
      password: new FormControl('', Validators.required),
      scope: new FormControl('nj', Validators.required)
    });
  }

  onSubmit() {
    this.authJwtService.login(this.loginForm.value).subscribe((v) => {
      console.log(v);
    });
  }
}
